﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using HWmanagementDB;

namespace HWmanagementServer
{
    /*
     * This class will handle server side logging 
     * Most logs will go to the database, but server specific logs might
     * go to a file on the server
     */
    class Logger : IDisposable
    {
        private StreamWriter logfile;

        //for storing the last id, must be set in constructor
        private int lastid;

        public Logger()
        {
            //Opening file for appending
            logfile = new StreamWriter("serverlog.log",true);
            
            //getting the last id form the DB
            using (Repository repo = new Repository())
            {
                try
                {
                    IEnumerator<LOG> LogEnum = repo.LogRepository.Get(order: (u) => u.OrderByDescending(
                                                                        (u2) => u2.ID)
                                                                    ).GetEnumerator();
                    LogEnum.MoveNext();
                    lastid = LogEnum.Current.ID;
                }
                catch (NullReferenceException)
                {
                    //if null is returned by the query
                    lastid = 1; 
                }
            }
        }

        //logging to file -- TESTED, OK
        public void LogToFile(string Message)
        {
            try
            {
                logfile.WriteLine(DateTime.Now.ToString() + ": " + Message);
                logfile.Flush();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return;
        }

        //logging to the database -- TESTED, OK
        public void LogDatabase(string Neptun, string Action)
        {
            //need to add the date and time of the logging as well
            string date = DateTime.Now.ToString();

            //dll call here, pushing the log to the database
            try
            {
                using (Repository repo = new Repository())
                {
                    repo.LogRepository.Insert(new LOG() { ID = lastid + 1, USERID = Neptun, DATE = DateTime.Now, ACTION = Action });
                    lastid++;
                }
            }
            catch
            {
                this.LogToFile("Error Logging to db!");
                throw new Exception("Error while handling request!");
            }
        }

        //returns all log entries form the DB (could be static)
        public List<LogModel> getLogs()
        {
            try
            {
                using (Repository repo = new Repository())
                {
                    IEnumerator<LOG> logs = repo.LogRepository.Get().GetEnumerator();
                    List<LogModel> ret = new List<LogModel>();
                    while (logs.MoveNext())
                    {
                        ret.Add(LogModel.fromDBTable(logs.Current));
                    }
                    return ret;
                }
            }
            catch
            {
                this.LogToFile("Error getting the logs form db!");
                throw new Exception("Error while handling request!");
            }
        }

        //closing the file
        public void Dispose()
        {
            logfile.Close();
        }
    }
}